class Solution:
    def canCompleteCircuit(self, gas, cost):
        n = len(gas)
        i = 0
        while i < n:
            # 枚举每个点 i
            left = 0
            j = 0
            while j < n:
                # 枚举从 i 开始向前走
                k = (i + j) % n
                # 补油再尝试向前走
                left += gas[k] - cost[k]
                if left < 0:
                    break
                j += 1
            # 等于 n 说明走完了 n 个点
            if j == n:
                return i
            # 否则 i 到 i + j 都不行，只能从 i + j + 1 枚举
            i = i + j + 1
        return -1